<template>
    <div class="body-container">
        <div class="back-button" @click="goBack">
            <i class="fa fa-arrow-left"></i>
            返回
        </div>
        <div class="title-section">
            <h1 class="main-title">中医体质测试</h1>
            <h1 class="subtitle">点击小球选择3项或更多，如没有以下症状则直接下一步</h1>
        </div>
        <div class="floating-container">
            <div v-for="item in testItems" 
                 :key="item.id" 
                 class="floating-ball"
                 :class="{ 'selected': selectedItems.includes(item) }"
                 :style="getBallStyle(item)"
                 @click="toggleSelection(item)">
                <div class="ball-content">
                    <div class="checkbox" v-if="selectedItems.includes(item)">✓</div>
                    <span class="ball-text">{{ item.content }}</span>
                </div>
            </div>
        </div>
        <button class="start-test-btn" @click="startTest">立即测试</button>
        <div class="footer">

        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const testItems = ref([])
const selectedItems = ref([])
const rotationAngle = ref(0)

const goBack = () => {
    router.back()
}

// 从后端获取问题
const fetchQuestions = async () => {
    try {
        const response = await axios.get('http://localhost:8081/question/get')
        if (response.data.code === 200) {
            testItems.value = response.data.data
            // 启动旋转动画
            startRotation()
        }
    } catch (error) {
        console.error('获取问题失败:', error)
    }
}

// 启动旋转动画
const startRotation = () => {
    const rotate = () => {
        rotationAngle.value = (rotationAngle.value + 0.02) % 360
        requestAnimationFrame(rotate)
    }
    rotate()
}

// 获取圆球样式
const getBallStyle = (item) => {
    const index = testItems.value.findIndex(i => i.id === item.id)
    const total = testItems.value.length
    const angleStep = (Math.PI * 2) / total
    const radius = Math.min(40, Math.max(30, total * 2)) - 5
    const centerX = 50
    const centerY = 50
    // 计算当前圆球的角度，加上旋转角度
    const angle = index * angleStep + (rotationAngle.value * Math.PI / 180)
    
    return {
        left: `${centerX + Math.cos(angle) * radius}%`,
        top: `${centerY + Math.sin(angle) * radius}%`,
        transform: 'translate(-50%, -50%)'
    }
}

// 切换选择状态
const toggleSelection = (item) => {
    const index = selectedItems.value.findIndex(selected => selected.id === item.id)
    if (index === -1) {
        selectedItems.value.push(item)
    } else {
        selectedItems.value.splice(index, 1)
    }
}

// 开始测试
const startTest = () => {
    console.log('已选择的项目:', selectedItems.value)
    const quesitons = []
    for (const item of selectedItems.value) {
        quesitons.push(item.content)
    }
    console.log(quesitons)
    axios.post('http://localhost:8081/question/getPhysique', {
        "question": quesitons
    }).then(response => {
        console.log(response)
        router.push({
            path: '/index',
            query: {
                physique: response.data.data
            }
        })
    })
}

onMounted(() => {
    fetchQuestions()
})
</script>

<style scoped>
.body-container {
    min-height: 100vh;
    padding: 0px;
    background: linear-gradient(135deg, #1a1f2c 0%, #2d3748 100%);
    color: #fff;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    font-family: 'Arial', sans-serif;
}

.title-section {
    margin-top: 5vh;
    margin-bottom: 50px;
    text-align: center;
}

.main-title {
    font-size: 64px;
    font-weight: bold;
    margin: 0 0 20px;
    background: linear-gradient(45deg, #f8e8ba, #d09d54);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
    animation: titleFadeIn 1s ease-in-out;
    
  font-family: 'STXingkai', 'Arial', sans-serif;
  letter-spacing: 1px;
}

.subtitle {
    font-size: 28px;
    margin: 0px 0 20px;
    line-height: 1.8;
    text-align: center;
    background: linear-gradient(45deg, #f8e8ba, #d09d54);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  /* font-family: 'STXingkai', 'Arial', sans-serif;
  letter-spacing: 1px; */
    animation: subtitleFadeIn 1s ease-in-out 0.2s forwards;
    opacity: 0;
}

@keyframes titleFadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.floating-container {
    position: relative;
    width: 100%;
    height: 55vh;
    min-height: 400px;

    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

.floating-ball {
    position: absolute;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.2) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.4s ease;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    transform-origin: center;
    z-index: 1;
}

.floating-ball:hover {
    transform: translate(-50%, -50%) scale(1.1) !important;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
    z-index: 2;
}

.floating-ball.selected {
    background: linear-gradient(135deg, rgba(0, 255, 255, 0.2) 0%, rgba(0, 255, 255, 0.3) 100%);
    border: 2px solid rgba(0, 255, 255, 0.5);
}

.ball-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 10px;
    text-align: center;
}

.checkbox {
    width: 24px;
    height: 24px;
    background: rgba(0, 255, 255, 0.3);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 5px;
    color: rgba(0, 255, 255, 0.8);
    font-weight: bold;
}

.ball-text {
    font-size: 16px;
    color: #fff;
    word-break: break-word;
    line-height: 1.2;
    padding: 0 5px;
}

.start-test-btn {
    margin-top: 20px;
    margin-bottom: 50px;
    padding: 15px 30px;
    border: none;
    border-radius: 20px;
    background: linear-gradient(45deg, #f8e8ba, #d09d54);
    color: white;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 10;
    position: relative;
}

.start-test-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
}

.footer {
    width: 100%;
    text-align: center;
    padding: 10px 0;
    background-color: rgba(0, 0, 0, 0.3);
    box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.2);
    color: #e6e6e6;
    font-size: 16px;
    margin-top: 50px;
    margin-left: 50px;
    box-sizing: border-box;
}

@keyframes graphFadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes subtitleFadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.back-button {
    position: fixed;
    top: 20px;
    left: 20px;
    padding: 10px 20px;
    background: linear-gradient(135deg, rgba(0, 255, 255, 0.2) 0%, rgba(0, 255, 255, 0.3) 100%);
    color: #fff;
    border-radius: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.3s ease;
    z-index: 100;
    font-size: 16px;
}

.back-button:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}
</style>